﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string bank_Add(bank model ,int ACID);


[OperationContract]
string bank_Update(bank model ,int ACID);


[OperationContract]
string bankBybankid_Delete(int bankid,int ACID);


[OperationContract]
bank bankBybankid_Get(int bankid,int ACID );


[OperationContract]
List<bank> bank_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加bank表数据
/// <summary>
/// 添加bank
/// </summary>
/// <param name="model">bank实体</param>
/// <returns>空或错误</returns>
public string bank_Add(bank model,int ACID)
{
if(model.bankid==null )
{
return "『银行帐号id』不能为空！";
}
if(string.IsNullOrEmpty(model.flag))
{
return "『存款类别』不能为空！";
}
if(model.balinit==null )
{
return "『开帐余额』不能为空！";
}
if(model.balance==null )
{
return "『当前余额』不能为空！";
}
if(string.IsNullOrEmpty(model.name))
{
return "『帐号名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.bank.Count(p=>p.bankid==model.bankid)>0)
{
return "主键已存在！";
}
ctx.bank.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加bank表数据事物用重构
/// <summary>
/// 添加bank事物用重构
/// </summary>
/// <param name="model">bank实体</param>
/// <returns>空或错误</returns>
private string bank_Add(bank model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(model.bankid==null )
{
return "『银行帐号id』不能为空！";
}
if(string.IsNullOrEmpty(model.flag))
{
return "『存款类别』不能为空！";
}
if(model.balinit==null )
{
return "『开帐余额』不能为空！";
}
if(model.balance==null )
{
return "『当前余额』不能为空！";
}
if(string.IsNullOrEmpty(model.name))
{
return "『帐号名称』不能为空！";
}
if(ctx.bank.Count(p=>p.bankid==model.bankid)>0)
{
return "主键已存在！";
}
ctx.bank.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改bank表数据
/// <summary>
/// 修改bank
/// </summary>
/// <param name="model">bank实体</param>
/// <returns>空或错误</returns>
public string bank_Update(bank model,int ACID)
{
if(model.bankid==null )
{
return "『银行帐号id』不能为空！";
}
if(string.IsNullOrEmpty(model.flag))
{
return "『存款类别』不能为空！";
}
if(model.balinit==null )
{
return "『开帐余额』不能为空！";
}
if(model.balance==null )
{
return "『当前余额』不能为空！";
}
if(string.IsNullOrEmpty(model.name))
{
return "『帐号名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.bank.Count(p=>p.bankid==model.bankid)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
bank bankmodel=ctx.bank.Single(p => p.bankid==model.bankid);
if(model.bankid!=null )
{
bankmodel.bankid=model.bankid;
}
if(!string.IsNullOrEmpty(model.name))
{
bankmodel.name=model.name;
}
if(!string.IsNullOrEmpty(model.bankname))
{
bankmodel.bankname=model.bankname;
}
if(!string.IsNullOrEmpty(model.bankcode))
{
bankmodel.bankcode=model.bankcode;
}
if(model.balance!=null )
{
bankmodel.balance=model.balance;
}
if(model.balinit!=null )
{
bankmodel.balinit=model.balinit;
}
if(!string.IsNullOrEmpty(model.subcode))
{
bankmodel.subcode=model.subcode;
}
if(!string.IsNullOrEmpty(model.flag))
{
bankmodel.flag=model.flag;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除bank表Bybankid数据
/// <summary>
/// 删除bankBybankid
/// </summary>
/// <param name="bankid">银行帐号id</param>
/// <returns>空或错误</returns>
public string bankBybankid_Delete(int bankid,int ACID)
{
if(bankid==null )
{
return "『银行帐号id』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.bank.Count(p=>p.bankid==bankid)!=1)
{
return null;
}
bank model=ctx.bank.Single(p => p.bankid== bankid);
ctx.bank.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除bank表Bybankid数据[事物用重构]
/// <summary>
/// 删除bankBybankid[事物用重构]
/// </summary>
/// <param name="bankid">银行帐号id</param>
/// <returns>空或错误</returns>
private string bankBybankid_Delete(int bankid,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(bankid==null )
{
return "『银行帐号id』不能为空！";
}
if(ctx.bank.Count(p=>p.bankid==bankid)!=1)
{
return null;
}
bank model=ctx.bank.Single(p => p.bankid== bankid);
ctx.bank.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取bank表Bybankid数据
/// <summary>
/// 获取bankBybankid
/// </summary>
/// <param name="bankid">银行帐号id</param>
/// <returns>当前表</returns>
public bank bankBybankid_Get(int bankid,int ACID)
{
if(bankid==null )
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.bank.Count(p=>p.bankid==bankid)!=1)
{
return null;
}
return ctx.bank.SingleOrDefault(p=>p.bankid==bankid);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取bank表数据
/// <summary>
/// 获取bank
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<bank></returns>
public List<bank> bank_Get(Expression<Func<bank, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.bank.ToList();
}
 else 
{
return ctx.bank.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.bank.Where(predicate).ToList();
}
 else 
{
return ctx.bank.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取bank表数据
/// <summary>
/// 获取bank
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<bank></returns>
public List<bank> bank_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return bank_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取bank表数据计数
/// <summary>
/// 获取bank表数据计数
/// </summary>
/// <returns>List<bank></returns>
public int bank_GetCount(Expression<Func<bank, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.bank.Count();

}
 else
{
return ctx.bank.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取bank分页表数据
/// <summary>
/// 获取bank分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<bank></returns>
public List<bank> bank_Getpage(int pageIndex, int pageSize,Expression<Func<bank, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.bank.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.bank.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.bank.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.bank.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改bank表数据
/// <summary>
/// 批量修改bank
/// </summary>
/// <param name="predicate">Expression<Func<bank, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<bank,bank>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int bank_batchUpdate(Expression<Func<bank, bool>> predicate,Expression<Func<bank, bank>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.bank.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改bank表数据事物用重构
/// <summary>
/// 批量修改bank事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<bank, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<bank,bank>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int bank_batchUpdate(Expression<Func<bank, bool>> predicate,Expression<Func<bank, bank>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.bank.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除bank表数据
/// <summary>
/// 批量删除bank
/// </summary>
/// <param name="predicate">Expression<Func<bank, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int bank_batchDel(Expression<Func<bank, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.bank.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除bank表数据事物用重构
/// <summary>
/// 批量删除bank事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<bank, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int bank_batchDel(Expression<Func<bank, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.bank.Delete(predicate);
return ok;
}
#endregion


}
}

